var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
// const { createProxyMiddleware } = require('http-proxy-middleware')

var indexRouter = require('./routes/index');
var auditRouter = require('./routes/audit');
var bankRouter = require('./routes/bank');
var categoryServiceRouter = require('./routes/category_service');
var categoriesRouter = require('./routes/category');
var channelRouter = require('./routes/channel');
var childrenRouter = require('./routes/children');
var circulationRouter = require('./routes/circulation');
var customerRouter = require('./routes/customer');
var departmentsRouter = require('./routes/department');
var headersRouter = require('./routes/header');
var locationRouter = require('./routes/location');
var loginsRouter = require('./routes/login');
var recordsRouter = require('./routes/record');
var rightsRouter = require('./routes/right');
var riskRouter = require('./routes/risk');
var roleRightRouter = require('./routes/role_right');
var rolesRouter = require('./routes/role');
var servicesRouter = require('./routes/service');
var typeRouter = require('./routes/type');
var usersRouter = require('./routes/user');

var app = express();
const cors = require("cors");
app.use(cors()); //使用cors中间件
// app.use('/api', createProxyMiddleware({
//   target: "http://localhost:5000",
//   changeOrigin: true,
//   pathRewrite: { '^/api': '' }
// }))

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/audit', auditRouter);
app.use('/bank', bankRouter);
app.use('/category_service', categoryServiceRouter);
app.use('/category', categoriesRouter);
app.use('/channel', channelRouter);
app.use('/children', childrenRouter);
app.use('/circulation', circulationRouter);
app.use('/customer', customerRouter);
app.use('/department', departmentsRouter);
app.use('/header', headersRouter);
app.use('/location', locationRouter);
app.use('/login', loginsRouter);
app.use('/record', recordsRouter);
app.use('/right', rightsRouter);
app.use('/risk', riskRouter);
app.use('/role_right', roleRightRouter);
app.use('/role', rolesRouter);
app.use('/service', servicesRouter);
app.use('/type', typeRouter);
app.use('/user', usersRouter);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
